package com.hzhr.iot.cqgas.ds.service;

import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.cqgas.ds.controller.StatisController;
import com.hzhr.iot.cqgas.ds.dao.device.custom.DevCollectDataCustomDao;
import com.hzhr.iot.cqgas.ds.dao.device.example.DevCollectDataDao;
import com.hzhr.iot.cqgas.ds.dao.device.example.DevIotDeviceDao;
import com.hzhr.iot.cqgas.ds.dao.statis.custom.*;
import com.hzhr.iot.cqgas.ds.dao.statis.example.*;
import com.hzhr.iot.cqgas.ds.entity.device.DevCollectDataEntity;
import com.hzhr.iot.cqgas.ds.entity.device.DevCollectDataExample;
import com.hzhr.iot.cqgas.ds.entity.device.DevCollectDeviceEntity;
import com.hzhr.iot.cqgas.ds.entity.device.dto.DevCollectDeviceEntityDto;
import com.hzhr.iot.cqgas.ds.entity.statis.*;
import com.hzhr.iot.cqgas.ds.entity.toll.TollGasUserEntity;
import com.hzhr.iot.cqgas.ds.entity.toll.dto.TollGasUserEntityDto;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptEntity;
import com.hzhr.iot.cqgas.ds.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Service
public class StatisService {
    @Autowired
    DevIotDeviceDao devIotDeviceDao;
    @Autowired
    DevCollectDataDao devCollectDataDao;
    @Autowired
    StatisDeviceDailyGasConsumeDao statisDeviceDailyGasConsumeDao;
    @Autowired
    StatisGasUserDailyGasConsumeDao statisGasUserDailyGasConsumeDao;
    @Autowired
    StatisDeptDailyGasConsumeDao statisDeptDailyGasConsumeDao;
    @Autowired
    StatisDeviceDailyGasConsumeCustomDao statisDeviceDailyGasConsumeCustomDao;
    @Autowired
    StatisGasUserDailyGasConsumeCustomDao statisGasUserDailyGasConsumeCustomDao;
    @Autowired
    StatisDeptDailyGasConsumeCustomDao statisDeptDailyGasConsumeCustomDao;
    @Autowired
    StatisDeviceMonthlyGasConsumeDao statisDeviceMonthlyGasConsumeDao;
    @Autowired
    StatisDeviceMonthlyGasConsumeCustomDao statisDeviceMonthlyGasConsumeCustomDao;
    @Autowired
    StatisGasUserMonthlyGasConsumeDao statisGasUserMonthlyGasConsumeDao;
    @Autowired
    StatisGasUserMonthlyGasConsumeCustomDao statisGasUserMonthlyGasConsumeCustomDao;
    @Autowired
    StatisDeptMonthlyGasConsumeDao statisDeptMonthlyGasConsumeDao;
    @Autowired
    StatisDeptMonthlyGasConsumeCustomDao statisDeptMonthlyGasConsumeCustomDao;
    @Autowired
    StatisOptionsService statisOptionsService;
    @Autowired
    TollOptionsService tollOptionsService;
    @Autowired
    DevOptionsService devOptionsService;
    @Autowired
    DevCollectDataCustomDao devCollectDataCustomDao;
    @Autowired
    StatisOptionsDao statisOptionsDao;
    @Autowired
    StatisDeviceDataExceptionDao statisDeviceDataExceptionDao;
    @Autowired
    StatisDeviceDataExceptionCustomDao statisDeviceDataExceptionCustomDao;

    @Autowired
    RedisUtil redisUtil;

    private Logger logger = LoggerFactory.getLogger(StatisService.class);

    @Async
    public Future<Map<String, Integer>> syncDeviceStatis(int ti, Integer day, List<DevCollectDeviceEntity> devices) {
        String gasPressureMonitorCode = devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "GAS_PRESSURE_MONITOR");

        int length = 500;
        int threadCount = 10;

        Date startDate = HzhrDateUtil.getDayStartTime(HzhrDateUtil.addDay(new Date(), -day));

        //设备日用气量统计
        int deviceDailyGasConsumeUpdateCount = 0;
        int deviceDailyGasConsumeInsertCount = 0;
        for (int i = ti - 1; ;i += threadCount) {
            try {
                List<StatisDeviceDailyGasConsumeEntity> updateDeviceDailyStatis = new ArrayList<>();
                List<StatisDeviceDailyGasConsumeEntity> insertDeviceDailyStatis = new ArrayList<>();

                List<DevCollectDeviceEntity> deviceListTmp = new ArrayList<>();
                for(int j = i * length + 1; j <= i * length + length && j < devices.size(); j++) {
                    DevCollectDeviceEntity device = devices.get(j);
                    deviceListTmp.add(device);
                }

                //剔除非燃气表
                List<DevCollectDeviceEntity> deviceList = new ArrayList<>();
                for (DevCollectDeviceEntity deviceTmp:
                        deviceListTmp) {
                    if (!gasPressureMonitorCode.equals(deviceTmp.getCategoryCode())) {
                        deviceList.add(deviceTmp);
                    }
                }

                for (int j = 0; j < deviceList.size(); j++) {
                    DevCollectDeviceEntity device = deviceList.get(j);
                    Integer deviceId = device.getId();
                    Integer iotDeviceId = device.getIotDeviceId();
                    Integer gasUserId = device.getGasUserId();
                    Integer deptId = device.getDeptId();
                    Integer companyId = device.getCompanyId();
                    Date date = startDate;

                    for (; date.getTime() <= new Date().getTime(); date = HzhrDateUtil.addDay(date, 1) ) {
                        Double data = getGasConsumeByDayAndCollectDeviceId(date, deviceId);
                        String dataStatus = "1";

                        if (data != null) {
                            StatisDeviceDailyGasConsumeExample statisDeviceDailyGasConsumeExample = new StatisDeviceDailyGasConsumeExample();
                            statisDeviceDailyGasConsumeExample.createCriteria().andCollectDeviceIdEqualTo(deviceId).andDateEqualTo(date);
                            List<StatisDeviceDailyGasConsumeEntity> statisDeviceDailyGasConsumeEntities = statisDeviceDailyGasConsumeDao.selectByExample(statisDeviceDailyGasConsumeExample);

                            if (statisDeviceDailyGasConsumeEntities.size() > 0) {
//                                StatisDeviceDailyGasConsumeEntity statisDeviceDailyGasConsumeEntity = statisDeviceDailyGasConsumeEntities.get(0);
//                                statisDeviceDailyGasConsumeEntity.setIotDeviceId(iotDeviceId);
//                                statisDeviceDailyGasConsumeEntity.setGasUserId(gasUserId);
//                                statisDeviceDailyGasConsumeEntity.setDeptId(deptId);
//                                statisDeviceDailyGasConsumeEntity.setCompanyId(companyId);
//                                statisDeviceDailyGasConsumeEntity.setData(data);
//                                updateDeviceDailyStatis.add(statisDeviceDailyGasConsumeEntity);
//                                deviceDailyGasConsumeUpdateCount++;
//
//                                if (updateDeviceDailyStatis.size() == 500) {
//                                    statisDeviceDailyGasConsumeCustomDao.updateBatch(updateDeviceDailyStatis);
//                                    updateDeviceDailyStatis = new ArrayList<>();
//                                }
                            } else {
                                StatisDeviceDailyGasConsumeEntity statisDeviceDailyGasConsumeEntity = new StatisDeviceDailyGasConsumeEntity();
                                statisDeviceDailyGasConsumeEntity.setCollectDeviceId(deviceId);
                                statisDeviceDailyGasConsumeEntity.setIotDeviceId(iotDeviceId);
                                statisDeviceDailyGasConsumeEntity.setGasUserId(gasUserId);
                                statisDeviceDailyGasConsumeEntity.setDeptId(deptId);
                                statisDeviceDailyGasConsumeEntity.setCompanyId(companyId);
                                statisDeviceDailyGasConsumeEntity.setData(data);
                                statisDeviceDailyGasConsumeEntity.setDate(date);
                                statisDeviceDailyGasConsumeEntity.setDataStatus(dataStatus);
                                insertDeviceDailyStatis.add(statisDeviceDailyGasConsumeEntity);
                                deviceDailyGasConsumeInsertCount++;

                                if (insertDeviceDailyStatis.size() == 500) {
                                    statisDeviceDailyGasConsumeCustomDao.insertBatch(quchong1(insertDeviceDailyStatis));
                                    insertDeviceDailyStatis = new ArrayList<>();
                                }
                            }
                        }
                    }
                }

                if (insertDeviceDailyStatis.size() > 0) {
                    statisDeviceDailyGasConsumeCustomDao.insertBatch(quchong1(insertDeviceDailyStatis));
                }
                if (updateDeviceDailyStatis.size() > 0) {
                    statisDeviceDailyGasConsumeCustomDao.updateBatch(quchong1(updateDeviceDailyStatis));
                }

                if (deviceListTmp.size() < length) break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        //设备月用气量统计
        int deviceMonthlyGasConsumeUpdateCount = 0;
        int deviceMonthlyGasConsumeInsertCount = 0;
        for (int i = ti - 1; ;i += threadCount) {
            try {
                List<StatisDeviceMonthlyGasConsumeEntity> updateDeviceMonthStatis = new ArrayList<>();
                List<StatisDeviceMonthlyGasConsumeEntity> insertDeviceMonthStatis = new ArrayList<>();

                List<DevCollectDeviceEntity> deviceListTmp = new ArrayList<>();
                for (int j = i * length + 1; j <= i * length + length && j < devices.size(); j++) {
                    DevCollectDeviceEntity device = devices.get(j);
                    deviceListTmp.add(device);
                }

                //剔除非燃气表
                List<DevCollectDeviceEntity> deviceList = new ArrayList<>();
                for (DevCollectDeviceEntity deviceTmp :
                        deviceListTmp) {
                    deviceList.add(deviceTmp);
                }

                for (DevCollectDeviceEntity device : deviceList) {
                    Integer deviceId = device.getId();
                    Integer iotDeviceId = device.getIotDeviceId();
                    Integer gasUserId = device.getGasUserId();
                    Integer deptId = device.getDeptId();
                    Integer companyId = device.getCompanyId();
                    Date date = startDate;

                    for (; date.getTime() <= new Date().getTime(); date = HzhrDateUtil.addMonth(date, 1) ) {
                        Date currentMonth = date;//当月
                        String currentMonthStr = HzhrDateUtil.dateToStr(currentMonth, "yyyy-MM") + "-01";
                        Date preMonth = HzhrDateUtil.addMonth(date, -1);//上个月
                        String preMonthStr = HzhrDateUtil.dateToStr(preMonth, "yyyy-MM") + "-01";
                        Date nextMonth = HzhrDateUtil.addMonth(date, 1);//下个月
                        String nextMonthStr = HzhrDateUtil.dateToStr(nextMonth, "yyyy-MM") + "-01";

                        Map<String, Object> criteriaMap = new HashMap<>();
                        criteriaMap.put("collectDeviceId", deviceId);
                        criteriaMap.put("startDate", preMonthStr);
                        criteriaMap.put("endDate", currentMonthStr);
                        Double preMonthGasConsume = statisDeviceDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//上个月用气量
                        if (preMonthGasConsume != null) {
                            String dataStatus = "1";

                            Date preMonthDate = HzhrDateUtil.strToDate(preMonthStr, "yyyy-MM-dd");

                            StatisDeviceMonthlyGasConsumeExample statisDeviceMonthlyGasConsumeExample = new StatisDeviceMonthlyGasConsumeExample();
                            statisDeviceMonthlyGasConsumeExample.createCriteria().andCollectDeviceIdEqualTo(deviceId).andDateEqualTo(preMonthDate);
                            List<StatisDeviceMonthlyGasConsumeEntity> statisDeviceMonthlyGasConsumeEntities = statisDeviceMonthlyGasConsumeDao.selectByExample(statisDeviceMonthlyGasConsumeExample);

                            if (statisDeviceMonthlyGasConsumeEntities.size() > 0) {
                                StatisDeviceMonthlyGasConsumeEntity statisDeviceMonthlyGasConsumeEntity = statisDeviceMonthlyGasConsumeEntities.get(0);
                                statisDeviceMonthlyGasConsumeEntity.setIotDeviceId(iotDeviceId);
                                statisDeviceMonthlyGasConsumeEntity.setGasUserId(gasUserId);
                                statisDeviceMonthlyGasConsumeEntity.setDeptId(deptId);
                                statisDeviceMonthlyGasConsumeEntity.setCompanyId(companyId);
                                statisDeviceMonthlyGasConsumeEntity.setData(preMonthGasConsume);
                                updateDeviceMonthStatis.add(statisDeviceMonthlyGasConsumeEntity);
                                deviceMonthlyGasConsumeUpdateCount++;

                                if (updateDeviceMonthStatis.size() == 500) {
                                    statisDeviceMonthlyGasConsumeCustomDao.updateBatch(quchong2(updateDeviceMonthStatis));
                                    updateDeviceMonthStatis = new ArrayList<>();
                                }
                            } else {
                                StatisDeviceMonthlyGasConsumeEntity statisDeviceMonthlyGasConsumeEntity = new StatisDeviceMonthlyGasConsumeEntity();
                                statisDeviceMonthlyGasConsumeEntity.setCollectDeviceId(deviceId);
                                statisDeviceMonthlyGasConsumeEntity.setIotDeviceId(iotDeviceId);
                                statisDeviceMonthlyGasConsumeEntity.setGasUserId(gasUserId);
                                statisDeviceMonthlyGasConsumeEntity.setDeptId(deptId);
                                statisDeviceMonthlyGasConsumeEntity.setCompanyId(companyId);
                                statisDeviceMonthlyGasConsumeEntity.setData(preMonthGasConsume);
                                statisDeviceMonthlyGasConsumeEntity.setDate(preMonthDate);
                                statisDeviceMonthlyGasConsumeEntity.setDataStatus(dataStatus);
                                insertDeviceMonthStatis.add(statisDeviceMonthlyGasConsumeEntity);
                                deviceMonthlyGasConsumeInsertCount++;

                                if (insertDeviceMonthStatis.size() == 500) {
                                    statisDeviceMonthlyGasConsumeCustomDao.insertBatch(quchong2(insertDeviceMonthStatis));
                                    insertDeviceMonthStatis = new ArrayList<>();
                                }
                            }
                        }

                        criteriaMap = new HashMap<>();
                        criteriaMap.put("collectDeviceId", deviceId);
                        criteriaMap.put("startDate", currentMonthStr);
                        criteriaMap.put("endDate", nextMonthStr);
                        Double currentMonthGasConsume = statisDeviceDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//当月用气量
                        if (currentMonthGasConsume != null) {
                            String dataStatus = "1";

                            Date currentMonthDate = HzhrDateUtil.strToDate(currentMonthStr, "yyyy-MM-dd");

                            StatisDeviceMonthlyGasConsumeExample statisDeviceMonthlyGasConsumeExample = new StatisDeviceMonthlyGasConsumeExample();
                            statisDeviceMonthlyGasConsumeExample.createCriteria().andCollectDeviceIdEqualTo(deviceId).andDateEqualTo(currentMonthDate);
                            List<StatisDeviceMonthlyGasConsumeEntity> statisDeviceMonthlyGasConsumeEntities = statisDeviceMonthlyGasConsumeDao.selectByExample(statisDeviceMonthlyGasConsumeExample);

                            if (statisDeviceMonthlyGasConsumeEntities.size() > 0) {
                                StatisDeviceMonthlyGasConsumeEntity statisDeviceMonthlyGasConsumeEntity = statisDeviceMonthlyGasConsumeEntities.get(0);
                                statisDeviceMonthlyGasConsumeEntity.setIotDeviceId(iotDeviceId);
                                statisDeviceMonthlyGasConsumeEntity.setGasUserId(gasUserId);
                                statisDeviceMonthlyGasConsumeEntity.setDeptId(deptId);
                                statisDeviceMonthlyGasConsumeEntity.setCompanyId(companyId);
                                statisDeviceMonthlyGasConsumeEntity.setData(currentMonthGasConsume);
                                updateDeviceMonthStatis.add(statisDeviceMonthlyGasConsumeEntity);
                                deviceMonthlyGasConsumeUpdateCount++;

                                if (updateDeviceMonthStatis.size() == 500) {
                                    statisDeviceMonthlyGasConsumeCustomDao.updateBatch(quchong2(updateDeviceMonthStatis));
                                    updateDeviceMonthStatis = new ArrayList<>();
                                }
                            } else {
                                StatisDeviceMonthlyGasConsumeEntity statisDeviceMonthlyGasConsumeEntity = new StatisDeviceMonthlyGasConsumeEntity();
                                statisDeviceMonthlyGasConsumeEntity.setCollectDeviceId(deviceId);
                                statisDeviceMonthlyGasConsumeEntity.setIotDeviceId(iotDeviceId);
                                statisDeviceMonthlyGasConsumeEntity.setGasUserId(gasUserId);
                                statisDeviceMonthlyGasConsumeEntity.setDeptId(deptId);
                                statisDeviceMonthlyGasConsumeEntity.setCompanyId(companyId);
                                statisDeviceMonthlyGasConsumeEntity.setData(currentMonthGasConsume);
                                statisDeviceMonthlyGasConsumeEntity.setDate(currentMonthDate);
                                statisDeviceMonthlyGasConsumeEntity.setDataStatus(dataStatus);
                                insertDeviceMonthStatis.add(statisDeviceMonthlyGasConsumeEntity);
                                deviceMonthlyGasConsumeInsertCount++;

                                if (insertDeviceMonthStatis.size() == 500) {
                                    statisDeviceMonthlyGasConsumeCustomDao.insertBatch(quchong2(insertDeviceMonthStatis));
                                    insertDeviceMonthStatis = new ArrayList<>();
                                }

                            }
                        }
                    }
                }

                if (insertDeviceMonthStatis.size() > 0) {
                    statisDeviceMonthlyGasConsumeCustomDao.insertBatch(quchong2(insertDeviceMonthStatis));
                }
                if (updateDeviceMonthStatis.size() > 0) {
                    statisDeviceMonthlyGasConsumeCustomDao.updateBatch(quchong2(updateDeviceMonthStatis));
                }

                if (deviceListTmp.size() < length) break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("deviceDailyGasConsumeUpdateCount", deviceDailyGasConsumeUpdateCount);
        resultMap.put("deviceDailyGasConsumeInsertCount", deviceDailyGasConsumeInsertCount);
        resultMap.put("deviceMonthlyGasConsumeUpdateCount", deviceMonthlyGasConsumeUpdateCount);
        resultMap.put("deviceMonthlyGasConsumeInsertCount", deviceMonthlyGasConsumeInsertCount);
        Future<Map<String, Integer>> future = new AsyncResult<Map<String, Integer>>(resultMap);// Future接收返回值，这里是String类型，可以指明其他类型
        return future;
    }
    @Async
    public Future<Map<String, Integer>> syncGasUserStatis(int ti, Integer day, List<TollGasUserEntity> gasUsers) {
        int length = 500;
        int threadCount = 10;

        Date startDate = HzhrDateUtil.getDayStartTime(HzhrDateUtil.addDay(new Date(), -day));

        //燃气用户日用气量统计
        int gasUserDailyGasConsumeUpdateCount = 0;
        int gasUserDailyGasConsumeInsertCount = 0;
        for (int i = ti - 1; ;i += threadCount) {
            try {
                List<StatisGasUserDailyGasConsumeEntity> updateGasUserDailyStatis = new ArrayList<>();
                List<StatisGasUserDailyGasConsumeEntity> insertGasUserDailyStatis = new ArrayList<>();

                List<TollGasUserEntity> gasUserList = new ArrayList<>();
                for (int j = i * length + 1; j <= i * length + length && j < gasUsers.size(); j++) {
                    TollGasUserEntity gasUser = gasUsers.get(j);
                    gasUserList.add(gasUser);
                }

                for (TollGasUserEntity gasUser : gasUserList) {
                    Integer gasUserId = gasUser.getId();
                    String gasUserType = gasUser.getTypeCode();
                    Integer deptId = gasUser.getDeptId();
                    Integer companyId = gasUser.getCompanyId();
                    Date date = startDate;

                    for (; date.getTime() <= new Date().getTime(); date = HzhrDateUtil.addDay(date, 1) ) {
                        String startDateStr = HzhrDateUtil.dateToStr(date);
                        String endDateStr = HzhrDateUtil.addDay(startDateStr, 1, "yyyy-MM-dd HH:mm:ss");

                        Map<String, Object> criteriaMap = new HashMap<>();
                        criteriaMap.put("gasUserId", gasUserId);
                        criteriaMap.put("startDate", startDateStr);
                        criteriaMap.put("endDate", endDateStr);
                        Double data = statisDeviceDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//当天用气量
                        String dataStatus = "1";

                        if (data != null) {
                            StatisGasUserDailyGasConsumeExample statisGasUserDailyGasConsumeExample = new StatisGasUserDailyGasConsumeExample();
                            statisGasUserDailyGasConsumeExample.createCriteria().andGasUserIdEqualTo(gasUserId).andDateEqualTo(date);
                            List<StatisGasUserDailyGasConsumeEntity> statisGasUserDailyGasConsumeEntities = statisGasUserDailyGasConsumeDao.selectByExample(statisGasUserDailyGasConsumeExample);

                            if (statisGasUserDailyGasConsumeEntities.size() > 0) {
                                StatisGasUserDailyGasConsumeEntity statisGasUserDailyGasConsumeEntity = statisGasUserDailyGasConsumeEntities.get(0);
                                statisGasUserDailyGasConsumeEntity.setGasUserId(gasUserId);
                                statisGasUserDailyGasConsumeEntity.setGasUserType(gasUserType);
                                statisGasUserDailyGasConsumeEntity.setDeptId(deptId);
                                statisGasUserDailyGasConsumeEntity.setCompanyId(companyId);
                                updateGasUserDailyStatis.add(statisGasUserDailyGasConsumeEntity);
                                gasUserDailyGasConsumeUpdateCount++;

                                if (updateGasUserDailyStatis.size() == 500) {
                                    statisGasUserDailyGasConsumeCustomDao.updateBatch(quchong3(updateGasUserDailyStatis));
                                    updateGasUserDailyStatis = new ArrayList<>();
                                }
                            } else {
                                StatisGasUserDailyGasConsumeEntity statisGasUserDailyGasConsumeEntity = new StatisGasUserDailyGasConsumeEntity();
                                statisGasUserDailyGasConsumeEntity.setGasUserId(gasUserId);
                                statisGasUserDailyGasConsumeEntity.setGasUserType(gasUserType);
                                statisGasUserDailyGasConsumeEntity.setDeptId(deptId);
                                statisGasUserDailyGasConsumeEntity.setCompanyId(companyId);
                                statisGasUserDailyGasConsumeEntity.setData(data);
                                statisGasUserDailyGasConsumeEntity.setDate(date);
                                statisGasUserDailyGasConsumeEntity.setDataStatus(dataStatus);
                                insertGasUserDailyStatis.add(statisGasUserDailyGasConsumeEntity);
                                gasUserDailyGasConsumeInsertCount++;

                                if (insertGasUserDailyStatis.size() == 500) {
                                    statisGasUserDailyGasConsumeCustomDao.insertBatch(quchong3(insertGasUserDailyStatis));
                                    insertGasUserDailyStatis = new ArrayList<>();
                                }

                            }
                        }
                    }
                }

                if (insertGasUserDailyStatis.size() > 0) {
                    statisGasUserDailyGasConsumeCustomDao.insertBatch(quchong3(insertGasUserDailyStatis));
                }
                if (updateGasUserDailyStatis.size() > 0) {
                    statisGasUserDailyGasConsumeCustomDao.updateBatch(quchong3(updateGasUserDailyStatis));
                }

                if (gasUserList.size() < length) break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //燃气用户月用气量统计
        int gasUserMonthlyGasConsumeUpdateCount = 0;
        int gasUserMonthlyGasConsumeInsertCount = 0;
        for (int i = ti - 1; ;i += threadCount) {
            try {
                List<StatisGasUserMonthlyGasConsumeEntity> updateGasUserMonthlyStatis = new ArrayList<>();
                List<StatisGasUserMonthlyGasConsumeEntity> insertGasUserMonthlyStatis = new ArrayList<>();

                List<TollGasUserEntity> gasUserList = new ArrayList<>();
                for (int j = i * length + 1; j <= i * length + length && j < gasUsers.size(); j++) {
                    TollGasUserEntity gasUser = gasUsers.get(j);
                    gasUserList.add(gasUser);
                }

                for (TollGasUserEntity gasUser : gasUserList) {
                    Integer gasUserId = gasUser.getId();
                    String gasUserType = gasUser.getTypeCode();
                    Integer deptId = gasUser.getDeptId();
                    Integer companyId = gasUser.getCompanyId();
                    Date date = startDate;

                    for (; date.getTime() <= new Date().getTime(); date = HzhrDateUtil.addMonth(date, 1) ) {
                        Date currentMonth = date;//当月
                        String currentMonthStr = HzhrDateUtil.dateToStr(currentMonth, "yyyy-MM") + "-01";
                        Date preMonth = HzhrDateUtil.addMonth(date, -1);//上个月
                        String preMonthStr = HzhrDateUtil.dateToStr(preMonth, "yyyy-MM") + "-01";
                        Date nextMonth = HzhrDateUtil.addMonth(date, 1);//下个月
                        String nextMonthStr = HzhrDateUtil.dateToStr(nextMonth, "yyyy-MM") + "-01";

                        Map<String, Object> criteriaMap = new HashMap<>();
                        criteriaMap.put("gasUserId", gasUserId);
                        criteriaMap.put("startDate", preMonthStr);
                        criteriaMap.put("endDate", currentMonthStr);
                        Double preMonthGasConsume = statisGasUserDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//上个月用气量
                        if (preMonthGasConsume != null) {
                            String dataStatus = "1";

                            Date preMonthDate = HzhrDateUtil.strToDate(preMonthStr, "yyyy-MM-dd");

                            StatisGasUserMonthlyGasConsumeExample statisGasUserMonthlyGasConsumeExample = new StatisGasUserMonthlyGasConsumeExample();
                            statisGasUserMonthlyGasConsumeExample.createCriteria().andGasUserIdEqualTo(gasUserId).andDateEqualTo(preMonthDate);
                            List<StatisGasUserMonthlyGasConsumeEntity> statisGasUserMonthlyGasConsumeEntities = statisGasUserMonthlyGasConsumeDao.selectByExample(statisGasUserMonthlyGasConsumeExample);

                            if (statisGasUserMonthlyGasConsumeEntities.size() > 0) {
                                StatisGasUserMonthlyGasConsumeEntity statisGasUserMonthlyGasConsumeEntity = statisGasUserMonthlyGasConsumeEntities.get(0);
                                statisGasUserMonthlyGasConsumeEntity.setGasUserId(gasUserId);
                                statisGasUserMonthlyGasConsumeEntity.setGasUserType(gasUserType);
                                statisGasUserMonthlyGasConsumeEntity.setDeptId(deptId);
                                statisGasUserMonthlyGasConsumeEntity.setCompanyId(companyId);
                                statisGasUserMonthlyGasConsumeEntity.setData(preMonthGasConsume);
                                updateGasUserMonthlyStatis.add(statisGasUserMonthlyGasConsumeEntity);
                                gasUserMonthlyGasConsumeUpdateCount++;

                                if (updateGasUserMonthlyStatis.size() == 500) {
                                    statisGasUserMonthlyGasConsumeCustomDao.updateBatch(quchong4(updateGasUserMonthlyStatis));
                                    updateGasUserMonthlyStatis = new ArrayList<>();
                                }

                            } else {
                                StatisGasUserMonthlyGasConsumeEntity statisGasUserMonthlyGasConsumeEntity = new StatisGasUserMonthlyGasConsumeEntity();
                                statisGasUserMonthlyGasConsumeEntity.setGasUserId(gasUserId);
                                statisGasUserMonthlyGasConsumeEntity.setGasUserType(gasUserType);
                                statisGasUserMonthlyGasConsumeEntity.setDeptId(deptId);
                                statisGasUserMonthlyGasConsumeEntity.setCompanyId(companyId);
                                statisGasUserMonthlyGasConsumeEntity.setData(preMonthGasConsume);
                                statisGasUserMonthlyGasConsumeEntity.setDate(preMonthDate);
                                statisGasUserMonthlyGasConsumeEntity.setDataStatus(dataStatus);
                                insertGasUserMonthlyStatis.add(statisGasUserMonthlyGasConsumeEntity);
                                gasUserMonthlyGasConsumeInsertCount++;

                                if (insertGasUserMonthlyStatis.size() == 500) {
                                    statisGasUserMonthlyGasConsumeCustomDao.insertBatch(quchong4(insertGasUserMonthlyStatis));
                                    insertGasUserMonthlyStatis = new ArrayList<>();
                                }
                            }
                        }

                        criteriaMap = new HashMap<>();
                        criteriaMap.put("gasUserId", gasUserId);
                        criteriaMap.put("startDate", currentMonthStr);
                        criteriaMap.put("endDate", nextMonthStr);
                        Double currentMonthGasConsume = statisDeviceDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//当月用气量
                        if (currentMonthGasConsume != null) {
                            String dataStatus = "1";

                            Date currentMonthDate = HzhrDateUtil.strToDate(currentMonthStr, "yyyy-MM-dd");

                            StatisGasUserMonthlyGasConsumeExample statisGasUserMonthlyGasConsumeExample = new StatisGasUserMonthlyGasConsumeExample();
                            statisGasUserMonthlyGasConsumeExample.createCriteria().andGasUserIdEqualTo(gasUserId).andDateEqualTo(currentMonth);
                            List<StatisGasUserMonthlyGasConsumeEntity> statisGasUserMonthlyGasConsumeEntities = statisGasUserMonthlyGasConsumeDao.selectByExample(statisGasUserMonthlyGasConsumeExample);

                            if (statisGasUserMonthlyGasConsumeEntities.size() > 0) {
                                StatisGasUserMonthlyGasConsumeEntity statisGasUserMonthlyGasConsumeEntity = statisGasUserMonthlyGasConsumeEntities.get(0);
                                statisGasUserMonthlyGasConsumeEntity.setGasUserId(gasUserId);
                                statisGasUserMonthlyGasConsumeEntity.setDeptId(deptId);
                                statisGasUserMonthlyGasConsumeEntity.setCompanyId(companyId);
                                statisGasUserMonthlyGasConsumeEntity.setData(currentMonthGasConsume);
                                updateGasUserMonthlyStatis.add(statisGasUserMonthlyGasConsumeEntity);
                                gasUserMonthlyGasConsumeUpdateCount++;

                                if (updateGasUserMonthlyStatis.size() == 500) {
                                    statisGasUserMonthlyGasConsumeCustomDao.updateBatch(quchong4(updateGasUserMonthlyStatis));
                                    updateGasUserMonthlyStatis = new ArrayList<>();
                                }
                            } else {
                                StatisGasUserMonthlyGasConsumeEntity statisGasUserMonthlyGasConsumeEntity = new StatisGasUserMonthlyGasConsumeEntity();
                                statisGasUserMonthlyGasConsumeEntity.setGasUserId(gasUserId);
                                statisGasUserMonthlyGasConsumeEntity.setDeptId(deptId);
                                statisGasUserMonthlyGasConsumeEntity.setCompanyId(companyId);
                                statisGasUserMonthlyGasConsumeEntity.setData(currentMonthGasConsume);
                                statisGasUserMonthlyGasConsumeEntity.setDate(currentMonthDate);
                                statisGasUserMonthlyGasConsumeEntity.setDataStatus(dataStatus);
                                insertGasUserMonthlyStatis.add(statisGasUserMonthlyGasConsumeEntity);
                                gasUserMonthlyGasConsumeInsertCount++;

                                if (insertGasUserMonthlyStatis.size() == 500) {
                                    statisGasUserMonthlyGasConsumeCustomDao.insertBatch(quchong4(insertGasUserMonthlyStatis));
                                    insertGasUserMonthlyStatis = new ArrayList<>();
                                }
                            }
                        }
                    }
                }

                if (insertGasUserMonthlyStatis.size() > 0) {
                    statisGasUserMonthlyGasConsumeCustomDao.insertBatch(quchong4(insertGasUserMonthlyStatis));
                }
                if (updateGasUserMonthlyStatis.size() > 0) {
                    statisGasUserMonthlyGasConsumeCustomDao.updateBatch(quchong4(updateGasUserMonthlyStatis));
                }

                if (gasUserList.size() < length) break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("gasUserDailyGasConsumeUpdateCount", gasUserDailyGasConsumeUpdateCount);
        resultMap.put("gasUserDailyGasConsumeInsertCount", gasUserDailyGasConsumeInsertCount);
        resultMap.put("gasUserMonthlyGasConsumeUpdateCount", gasUserMonthlyGasConsumeUpdateCount);
        resultMap.put("gasUserMonthlyGasConsumeInsertCount", gasUserMonthlyGasConsumeInsertCount);
        Future<Map<String, Integer>> future = new AsyncResult<Map<String, Integer>>(resultMap);// Future接收返回值，这里是String类型，可以指明其他类型
        return future;
    }
    @Async
    public Future<Map<String, Integer>> syncDeptStatis(Integer day, List<UsrDeptEntity> depts) {
        int length = 500;
        int threadCount = 10;

        Date startDate = HzhrDateUtil.getDayStartTime(HzhrDateUtil.addDay(new Date(), -day));

        String residentCode = tollOptionsService.getCode("GAS_USER_TYPE", "RESIDENTIAL_USER");
        String commercialCode = tollOptionsService.getCode("GAS_USER_TYPE", "COMMERCIAL_USER");

        //部门日用气量统计
        int deptDailyGasConsumeUpdateCount = 0;
        int deptDailyGasConsumeInsertCount = 0;
        List<StatisDeptDailyGasConsumeEntity> updateDeptDailyStatis = new ArrayList<>();
        List<StatisDeptDailyGasConsumeEntity> insertDeptDailyStatis = new ArrayList<>();
        for (UsrDeptEntity dept : depts) {
            Integer deptId = dept.getId();
            Integer companyId = dept.getCompanyId();
            Date date = startDate;

            for (; date.getTime() <= new Date().getTime(); date = HzhrDateUtil.addDay(date, 1) ) {
                String startDateStr = HzhrDateUtil.dateToStr(date);
                String endDateStr = HzhrDateUtil.addDay(startDateStr, 1, "yyyy-MM-dd HH:mm:ss");

                //居民
                Map<String, Object> criteriaMap = new HashMap<>();
                criteriaMap.put("deptId", deptId);
                criteriaMap.put("gasUserType", residentCode);
                criteriaMap.put("startDate", startDateStr);
                criteriaMap.put("endDate", endDateStr);
                Double data = statisGasUserDailyGasConsumeCustomDao.selectDataSum(criteriaMap);
                String dataStatus = "1";

                if (data != null) {
                    StatisDeptDailyGasConsumeExample statisDeptDailyGasConsumeExample = new StatisDeptDailyGasConsumeExample();
                    statisDeptDailyGasConsumeExample.createCriteria().andDeptIdEqualTo(deptId).andGasUserTypeEqualTo(residentCode).andDateEqualTo(date);
                    List<StatisDeptDailyGasConsumeEntity> statisDeptDailyGasConsumeEntities = statisDeptDailyGasConsumeDao.selectByExample(statisDeptDailyGasConsumeExample);

                    if (statisDeptDailyGasConsumeEntities.size() > 0) {
                        StatisDeptDailyGasConsumeEntity statisDeptDailyGasConsumeEntity = statisDeptDailyGasConsumeEntities.get(0);
                        statisDeptDailyGasConsumeEntity.setDeptId(deptId);
                        statisDeptDailyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptDailyGasConsumeEntity.setData(data);
                        updateDeptDailyStatis.add(statisDeptDailyGasConsumeEntity);
                        deptDailyGasConsumeUpdateCount++;

                        if (updateDeptDailyStatis.size() == 500) {
                            statisDeptDailyGasConsumeCustomDao.updateBatch(quchong5(updateDeptDailyStatis));
                            updateDeptDailyStatis = new ArrayList<>();
                        }
                    } else {
                        StatisDeptDailyGasConsumeEntity statisDeptDailyGasConsumeEntity = new StatisDeptDailyGasConsumeEntity();
                        statisDeptDailyGasConsumeEntity.setDeptId(deptId);
                        statisDeptDailyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptDailyGasConsumeEntity.setGasUserType(residentCode);
                        statisDeptDailyGasConsumeEntity.setData(data);
                        statisDeptDailyGasConsumeEntity.setDate(date);
                        statisDeptDailyGasConsumeEntity.setDataStatus(dataStatus);
                        insertDeptDailyStatis.add(statisDeptDailyGasConsumeEntity);
                        deptDailyGasConsumeInsertCount++;

                        if (insertDeptDailyStatis.size() == 500) {
                            statisDeptDailyGasConsumeCustomDao.insertBatch(quchong5(insertDeptDailyStatis));
                            insertDeptDailyStatis = new ArrayList<>();
                        }
                    }
                }

                //工商
                criteriaMap = new HashMap<>();
                criteriaMap.put("deptId", deptId);
                criteriaMap.put("gasUserType", commercialCode);
                criteriaMap.put("startDate", startDateStr);
                criteriaMap.put("endDate", endDateStr);
                data = statisGasUserDailyGasConsumeCustomDao.selectDataSum(criteriaMap);
                dataStatus = "1";

                if (data != null) {
                    StatisDeptDailyGasConsumeExample statisDeptDailyGasConsumeExample = new StatisDeptDailyGasConsumeExample();
                    statisDeptDailyGasConsumeExample.createCriteria().andDeptIdEqualTo(deptId).andGasUserTypeEqualTo(commercialCode).andDateEqualTo(date);
                    List<StatisDeptDailyGasConsumeEntity> statisDeptDailyGasConsumeEntities = statisDeptDailyGasConsumeDao.selectByExample(statisDeptDailyGasConsumeExample);

                    if (statisDeptDailyGasConsumeEntities.size() > 0) {
                        StatisDeptDailyGasConsumeEntity statisDeptDailyGasConsumeEntity = statisDeptDailyGasConsumeEntities.get(0);
                        statisDeptDailyGasConsumeEntity.setDeptId(deptId);
                        statisDeptDailyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptDailyGasConsumeEntity.setData(data);
                        updateDeptDailyStatis.add(statisDeptDailyGasConsumeEntity);
                        deptDailyGasConsumeUpdateCount++;

                        if (updateDeptDailyStatis.size() == 500) {
                            statisDeptDailyGasConsumeCustomDao.updateBatch(quchong5(updateDeptDailyStatis));
                            updateDeptDailyStatis = new ArrayList<>();
                        }
                    } else {
                        StatisDeptDailyGasConsumeEntity statisDeptDailyGasConsumeEntity = new StatisDeptDailyGasConsumeEntity();
                        statisDeptDailyGasConsumeEntity.setDeptId(deptId);
                        statisDeptDailyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptDailyGasConsumeEntity.setGasUserType(commercialCode);
                        statisDeptDailyGasConsumeEntity.setData(data);
                        statisDeptDailyGasConsumeEntity.setDate(date);
                        statisDeptDailyGasConsumeEntity.setDataStatus(dataStatus);
                        insertDeptDailyStatis.add(statisDeptDailyGasConsumeEntity);
                        deptDailyGasConsumeInsertCount++;

                        if (insertDeptDailyStatis.size() == 500) {
                            statisDeptDailyGasConsumeCustomDao.insertBatch(quchong5(insertDeptDailyStatis));
                            insertDeptDailyStatis = new ArrayList<>();
                        }
                    }
                }
            }
        }
        if (insertDeptDailyStatis.size() > 0) {
            statisDeptDailyGasConsumeCustomDao.insertBatch(quchong5(insertDeptDailyStatis));
        }
        if (updateDeptDailyStatis.size() > 0) {
            statisDeptDailyGasConsumeCustomDao.updateBatch(quchong5(updateDeptDailyStatis));
        }

        //部门月用气量统计
        int deptMonthlyGasConsumeUpdateCount = 0;
        int deptMonthlyGasConsumeInsertCount = 0;
        List<StatisDeptMonthlyGasConsumeEntity> updateDeptMonthlyStatis = new ArrayList<>();
        List<StatisDeptMonthlyGasConsumeEntity> insertDeptMonthlyStatis = new ArrayList<>();
        for (UsrDeptEntity dept : depts) {
            Integer deptId = dept.getId();
            Integer companyId = dept.getCompanyId();
            Date date = startDate;

            for (; date.getTime() <= new Date().getTime(); date = HzhrDateUtil.addMonth(date, 1) ) {
                Date currentMonth = date;//当月
                String currentMonthStr = HzhrDateUtil.dateToStr(currentMonth, "yyyy-MM") + "-01";
                Date preMonth = HzhrDateUtil.addMonth(date, -1);//上个月
                String preMonthStr = HzhrDateUtil.dateToStr(preMonth, "yyyy-MM") + "-01";
                Date nextMonth = HzhrDateUtil.addMonth(date, 1);//下个月
                String nextMonthStr = HzhrDateUtil.dateToStr(nextMonth, "yyyy-MM") + "-01";

                //居民
                Map<String, Object> criteriaMap = new HashMap<>();
                criteriaMap.put("deptId", deptId);
                criteriaMap.put("gasUserType", residentCode);
                criteriaMap.put("startDate", preMonthStr);
                criteriaMap.put("endDate", currentMonthStr);
                Double preMonthGasConsume = statisDeptDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//上个月用气量
                if (preMonthGasConsume != null) {
                    String dataStatus = "1";

                    Date preMonthDate = HzhrDateUtil.strToDate(preMonthStr, "yyyy-MM-dd");

                    StatisDeptMonthlyGasConsumeExample statisDeptMonthlyGasConsumeExample = new StatisDeptMonthlyGasConsumeExample();
                    statisDeptMonthlyGasConsumeExample.createCriteria().andDeptIdEqualTo(deptId).andGasUserTypeEqualTo(residentCode).andDateEqualTo(preMonthDate);
                    List<StatisDeptMonthlyGasConsumeEntity> statisDeptMonthlyGasConsumeEntities = statisDeptMonthlyGasConsumeDao.selectByExample(statisDeptMonthlyGasConsumeExample);

                    if (statisDeptMonthlyGasConsumeEntities.size() > 0) {
                        StatisDeptMonthlyGasConsumeEntity statisDeptMonthlyGasConsumeEntity = statisDeptMonthlyGasConsumeEntities.get(0);
                        statisDeptMonthlyGasConsumeEntity.setDeptId(deptId);
                        statisDeptMonthlyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptMonthlyGasConsumeEntity.setData(preMonthGasConsume);
                        updateDeptMonthlyStatis.add(statisDeptMonthlyGasConsumeEntity);
                        deptMonthlyGasConsumeUpdateCount++;

                        if (updateDeptMonthlyStatis.size() == 500) {
                            statisDeptMonthlyGasConsumeCustomDao.updateBatch(quchong6(updateDeptMonthlyStatis));
                            updateDeptMonthlyStatis = new ArrayList<>();
                        }
                    } else {
                        StatisDeptMonthlyGasConsumeEntity statisDeptMonthlyGasConsumeEntity = new StatisDeptMonthlyGasConsumeEntity();
                        statisDeptMonthlyGasConsumeEntity.setDeptId(deptId);
                        statisDeptMonthlyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptMonthlyGasConsumeEntity.setGasUserType(residentCode);
                        statisDeptMonthlyGasConsumeEntity.setData(preMonthGasConsume);
                        statisDeptMonthlyGasConsumeEntity.setDate(preMonthDate);
                        statisDeptMonthlyGasConsumeEntity.setDataStatus(dataStatus);
                        insertDeptMonthlyStatis.add(statisDeptMonthlyGasConsumeEntity);
                        deptMonthlyGasConsumeInsertCount++;

                        if (insertDeptMonthlyStatis.size() == 500) {
                            statisDeptMonthlyGasConsumeCustomDao.insertBatch(quchong6(insertDeptMonthlyStatis));
                            insertDeptMonthlyStatis = new ArrayList<>();
                        }
                    }
                }

                criteriaMap = new HashMap<>();
                criteriaMap.put("deptId", deptId);
                criteriaMap.put("gasUserType", residentCode);
                criteriaMap.put("startDate", preMonthStr);
                criteriaMap.put("endDate", nextMonthStr);
                Double currentMonthGasConsume = statisDeptDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//当月用气量
                if (currentMonthGasConsume != null) {
                    String dataStatus = "1";

                    Date currentMonthDate = HzhrDateUtil.strToDate(currentMonthStr, "yyyy-MM-dd");

                    StatisDeptMonthlyGasConsumeExample statisDeptMonthlyGasConsumeExample = new StatisDeptMonthlyGasConsumeExample();
                    statisDeptMonthlyGasConsumeExample.createCriteria().andDeptIdEqualTo(deptId).andGasUserTypeEqualTo(residentCode).andDateEqualTo(currentMonthDate);
                    List<StatisDeptMonthlyGasConsumeEntity> statisDeptMonthlyGasConsumeEntities = statisDeptMonthlyGasConsumeDao.selectByExample(statisDeptMonthlyGasConsumeExample);

                    if (statisDeptMonthlyGasConsumeEntities.size() > 0) {
                        StatisDeptMonthlyGasConsumeEntity statisDeptMonthlyGasConsumeEntity = statisDeptMonthlyGasConsumeEntities.get(0);
                        statisDeptMonthlyGasConsumeEntity.setDeptId(deptId);
                        statisDeptMonthlyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptMonthlyGasConsumeEntity.setData(currentMonthGasConsume);
                        updateDeptMonthlyStatis.add(statisDeptMonthlyGasConsumeEntity);
                        deptMonthlyGasConsumeUpdateCount++;

                        if (updateDeptMonthlyStatis.size() == 500) {
                            statisDeptMonthlyGasConsumeCustomDao.updateBatch(quchong6(updateDeptMonthlyStatis));
                            updateDeptMonthlyStatis = new ArrayList<>();
                        }
                    } else {
                        StatisDeptMonthlyGasConsumeEntity statisDeptMonthlyGasConsumeEntity = new StatisDeptMonthlyGasConsumeEntity();
                        statisDeptMonthlyGasConsumeEntity.setDeptId(deptId);
                        statisDeptMonthlyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptMonthlyGasConsumeEntity.setGasUserType(residentCode);
                        statisDeptMonthlyGasConsumeEntity.setData(currentMonthGasConsume);
                        statisDeptMonthlyGasConsumeEntity.setDate(currentMonthDate);
                        statisDeptMonthlyGasConsumeEntity.setDataStatus(dataStatus);
                        insertDeptMonthlyStatis.add(statisDeptMonthlyGasConsumeEntity);
                        deptMonthlyGasConsumeInsertCount++;

                        if (insertDeptMonthlyStatis.size() == 500) {
                            statisDeptMonthlyGasConsumeCustomDao.insertBatch(quchong6(insertDeptMonthlyStatis));
                            insertDeptMonthlyStatis = new ArrayList<>();
                        }
                    }
                }

                //工商
                criteriaMap = new HashMap<>();
                criteriaMap.put("deptId", deptId);
                criteriaMap.put("gasUserType", commercialCode);
                criteriaMap.put("startDate", preMonthStr);
                criteriaMap.put("endDate", currentMonthStr);
                preMonthGasConsume = statisDeptDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//上个月用气量
                if (preMonthGasConsume != null) {
                    String dataStatus = "1";

                    Date preMonthDate = HzhrDateUtil.strToDate(preMonthStr, "yyyy-MM-dd");

                    StatisDeptMonthlyGasConsumeExample statisDeptMonthlyGasConsumeExample = new StatisDeptMonthlyGasConsumeExample();
                    statisDeptMonthlyGasConsumeExample.createCriteria().andDeptIdEqualTo(deptId).andGasUserTypeEqualTo(commercialCode).andDateEqualTo(preMonthDate);
                    List<StatisDeptMonthlyGasConsumeEntity> statisDeptMonthlyGasConsumeEntities = statisDeptMonthlyGasConsumeDao.selectByExample(statisDeptMonthlyGasConsumeExample);

                    if (statisDeptMonthlyGasConsumeEntities.size() > 0) {
                        StatisDeptMonthlyGasConsumeEntity statisDeptMonthlyGasConsumeEntity = statisDeptMonthlyGasConsumeEntities.get(0);
                        statisDeptMonthlyGasConsumeEntity.setDeptId(deptId);
                        statisDeptMonthlyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptMonthlyGasConsumeEntity.setData(preMonthGasConsume);
                        updateDeptMonthlyStatis.add(statisDeptMonthlyGasConsumeEntity);
                        deptMonthlyGasConsumeUpdateCount++;

                        if (updateDeptMonthlyStatis.size() == 500) {
                            statisDeptMonthlyGasConsumeCustomDao.updateBatch(quchong6(updateDeptMonthlyStatis));
                            updateDeptMonthlyStatis = new ArrayList<>();
                        }

                    } else {
                        StatisDeptMonthlyGasConsumeEntity statisDeptMonthlyGasConsumeEntity = new StatisDeptMonthlyGasConsumeEntity();
                        statisDeptMonthlyGasConsumeEntity.setDeptId(deptId);
                        statisDeptMonthlyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptMonthlyGasConsumeEntity.setGasUserType(commercialCode);
                        statisDeptMonthlyGasConsumeEntity.setData(preMonthGasConsume);
                        statisDeptMonthlyGasConsumeEntity.setDate(preMonthDate);
                        statisDeptMonthlyGasConsumeEntity.setDataStatus(dataStatus);
                        insertDeptMonthlyStatis.add(statisDeptMonthlyGasConsumeEntity);
                        deptMonthlyGasConsumeInsertCount++;

                        if (insertDeptMonthlyStatis.size() == 500) {
                            statisDeptMonthlyGasConsumeCustomDao.insertBatch(quchong6(insertDeptMonthlyStatis));
                            insertDeptMonthlyStatis = new ArrayList<>();
                        }
                    }
                }

                criteriaMap = new HashMap<>();
                criteriaMap.put("deptId", deptId);
                criteriaMap.put("gasUserType", commercialCode);
                criteriaMap.put("startDate", currentMonthStr);
                criteriaMap.put("endDate", nextMonthStr);
                currentMonthGasConsume = statisDeptDailyGasConsumeCustomDao.selectDataSum(criteriaMap);//当月用气量
                if (currentMonthGasConsume != null) {
                    String dataStatus = "1";

                    Date currentMonthDate = HzhrDateUtil.strToDate(currentMonthStr, "yyyy-MM-dd");

                    StatisDeptMonthlyGasConsumeExample statisDeptMonthlyGasConsumeExample = new StatisDeptMonthlyGasConsumeExample();
                    statisDeptMonthlyGasConsumeExample.createCriteria().andDeptIdEqualTo(deptId).andGasUserTypeEqualTo(commercialCode).andDateEqualTo(currentMonthDate);
                    List<StatisDeptMonthlyGasConsumeEntity> statisDeptMonthlyGasConsumeEntities = statisDeptMonthlyGasConsumeDao.selectByExample(statisDeptMonthlyGasConsumeExample);

                    if (statisDeptMonthlyGasConsumeEntities.size() > 0) {
                        StatisDeptMonthlyGasConsumeEntity statisDeptMonthlyGasConsumeEntity = statisDeptMonthlyGasConsumeEntities.get(0);
                        statisDeptMonthlyGasConsumeEntity.setDeptId(deptId);
                        statisDeptMonthlyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptMonthlyGasConsumeEntity.setData(currentMonthGasConsume);
                        updateDeptMonthlyStatis.add(statisDeptMonthlyGasConsumeEntity);
                        deptMonthlyGasConsumeUpdateCount++;

                        if (updateDeptMonthlyStatis.size() == 500) {
                            statisDeptMonthlyGasConsumeCustomDao.updateBatch(quchong6(updateDeptMonthlyStatis));
                            updateDeptMonthlyStatis = new ArrayList<>();
                        }
                    } else {
                        StatisDeptMonthlyGasConsumeEntity statisDeptMonthlyGasConsumeEntity = new StatisDeptMonthlyGasConsumeEntity();
                        statisDeptMonthlyGasConsumeEntity.setDeptId(deptId);
                        statisDeptMonthlyGasConsumeEntity.setCompanyId(companyId);
                        statisDeptMonthlyGasConsumeEntity.setGasUserType(commercialCode);
                        statisDeptMonthlyGasConsumeEntity.setData(currentMonthGasConsume);
                        statisDeptMonthlyGasConsumeEntity.setDate(currentMonthDate);
                        statisDeptMonthlyGasConsumeEntity.setDataStatus(dataStatus);
                        insertDeptMonthlyStatis.add(statisDeptMonthlyGasConsumeEntity);
                        deptMonthlyGasConsumeInsertCount++;

                        if (insertDeptMonthlyStatis.size() == 500) {
                            statisDeptMonthlyGasConsumeCustomDao.insertBatch(quchong6(insertDeptMonthlyStatis));
                            insertDeptMonthlyStatis = new ArrayList<>();
                        }
                    }
                }
            }
        }

        if (insertDeptMonthlyStatis.size() > 0) {
            statisDeptMonthlyGasConsumeCustomDao.insertBatch(quchong6(insertDeptMonthlyStatis));
        }
        if (updateDeptMonthlyStatis.size() > 0) {
            statisDeptMonthlyGasConsumeCustomDao.updateBatch(quchong6(updateDeptMonthlyStatis));
        }

        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("deptDailyGasConsumeUpdateCount", deptDailyGasConsumeUpdateCount);
        resultMap.put("deptDailyGasConsumeInsertCount", deptDailyGasConsumeInsertCount);
        resultMap.put("deptMonthlyGasConsumeUpdateCount", deptMonthlyGasConsumeUpdateCount);
        resultMap.put("deptMonthlyGasConsumeInsertCount", deptMonthlyGasConsumeInsertCount);
        Future<Map<String, Integer>> future = new AsyncResult<Map<String, Integer>>(resultMap);// Future接收返回值，这里是String类型，可以指明其他类型
        return future;
    }
    @Async
    public Future<Map<String, Integer>> syncDeviceDataException(List<Map<String, Object>> devices) {
        StatisOptionsExample example = new StatisOptionsExample();
        example.createCriteria().andTypeNameEqualTo("DEVICE_EXCEPTION_TYPE").andNameEqualTo("WSC");
        StatisOptionsEntity options1 = statisOptionsDao.selectByExample(example).get(0);
        String type1 = options1.getCode();//未上传
        example = new StatisOptionsExample();
        example.createCriteria().andTypeNameEqualTo("DEVICE_EXCEPTION_TYPE").andNameEqualTo("WYQ");
        StatisOptionsEntity options2 = statisOptionsDao.selectByExample(example).get(0);
        String type2 = options2.getCode();//未用气

        int wscCount = 0;
        int wyqCount = 0;

        List<StatisDeviceDataExceptionEntity> insert1 = new ArrayList<>();
        List<StatisDeviceDataExceptionEntity> insert2 = new ArrayList<>();
        for (Map<String, Object> device :
        devices) {
            try {
                Integer COLLECT_DEVICE_ID = HzhrTypeUtil.object2integer(device.get("COLLECT_DEVICE_ID"));
                Integer IOT_DEVICE_ID = HzhrTypeUtil.object2integer(device.get("IOT_DEVICE_ID"));
                String COLLECT_DEVICE_CODE = HzhrTypeUtil.object2string(device.get("COLLECT_DEVICE_CODE"));
                String COLLECT_DEVICE_CATEGORY_CODE = HzhrTypeUtil.object2string(device.get("COLLECT_DEVICE_CATEGORY_CODE"));
                Integer COLLECT_DEVICE_DEPT_ID = HzhrTypeUtil.object2integer(device.get("COLLECT_DEVICE_DEPT_ID"));
                String COLLECT_DEVICE_DEPT_NAME = HzhrTypeUtil.object2string(device.get("COLLECT_DEVICE_DEPT_NAME"));
                String COLLECT_DEVICE_RESOURCE_CODE = HzhrTypeUtil.object2string(device.get("COLLECT_DEVICE_RESOURCE_CODE"));
                String IOT_DEVICE_CODE = HzhrTypeUtil.object2string(device.get("IOT_DEVICE_CODE"));
                String IOT_DEVICE_CATEGORY_CODE = HzhrTypeUtil.object2string(device.get("IOT_DEVICE_CATEGORY_CODE"));
                Integer IOT_DEVICE_COMPANY_ID = HzhrTypeUtil.object2integer(device.get("IOT_DEVICE_COMPANY_ID"));
                String IOT_DEVICE_COMPANY_NAME = HzhrTypeUtil.object2string(device.get("IOT_DEVICE_COMPANY_NAME"));
                Integer GAS_USER_ID = HzhrTypeUtil.object2integer(device.get("GAS_USER_ID"));
                String GAS_USER_CODE = HzhrTypeUtil.object2string(device.get("GAS_USER_CODE"));

                //未上传
                Date date = devCollectDataCustomDao.select1(COLLECT_DEVICE_ID);
                if (date != null) {
                    int dayCount = HzhrDateUtil.differentDays(date, new Date());
                    if (dayCount <= 0) continue;
                    StatisDeviceDataExceptionExample exceptionExample = new StatisDeviceDataExceptionExample();
                    exceptionExample.createCriteria().andCollectDeviceIdEqualTo(COLLECT_DEVICE_ID).andExceptionTypeEqualTo(type1).andStatisDateEqualTo(new Date());
                    int count = statisDeviceDataExceptionDao.countByExample(exceptionExample);
                    if (count == 0) {
                        StatisDeviceDataExceptionEntity statisDeviceDataExceptionEntity = new StatisDeviceDataExceptionEntity();
                        statisDeviceDataExceptionEntity.setExceptionDayCount(dayCount > 99 ? 99 : dayCount);
                        statisDeviceDataExceptionEntity.setExceptionType(type1);
                        statisDeviceDataExceptionEntity.setStatisDate(new Date());
                        statisDeviceDataExceptionEntity.setCollectDeviceId(COLLECT_DEVICE_ID);
                        statisDeviceDataExceptionEntity.setCollectDeviceCode(COLLECT_DEVICE_CODE);
                        statisDeviceDataExceptionEntity.setCollectDeviceCategoryCode(COLLECT_DEVICE_CATEGORY_CODE);
                        statisDeviceDataExceptionEntity.setCollectDeviceDeptId(COLLECT_DEVICE_DEPT_ID);
                        statisDeviceDataExceptionEntity.setCollectDeviceDeptName(COLLECT_DEVICE_DEPT_NAME);
                        statisDeviceDataExceptionEntity.setCollectDeviceResourceCode(COLLECT_DEVICE_RESOURCE_CODE);
                        statisDeviceDataExceptionEntity.setIotDeviceId(IOT_DEVICE_ID);
                        statisDeviceDataExceptionEntity.setIotDeviceCode(IOT_DEVICE_CODE);
                        statisDeviceDataExceptionEntity.setIotDeviceCategoryCode(IOT_DEVICE_CATEGORY_CODE);
                        statisDeviceDataExceptionEntity.setIotDeviceCompanyId(IOT_DEVICE_COMPANY_ID);
                        statisDeviceDataExceptionEntity.setIotDeviceCompanyName(IOT_DEVICE_COMPANY_NAME);
                        statisDeviceDataExceptionEntity.setGasUserId(GAS_USER_ID);
                        statisDeviceDataExceptionEntity.setGasUserCode(GAS_USER_CODE);
                        insert1.add(statisDeviceDataExceptionEntity);
                        if (insert1.size() == 500) {
                            wscCount += statisDeviceDataExceptionCustomDao.insertBatch(insert1);
                            insert1 = new ArrayList<>();
                        }
                    }
                }

                //未用气
                Date date1 = statisDeviceDailyGasConsumeCustomDao.select1(COLLECT_DEVICE_ID);
                if (date1 == null) {
                    date1 = HzhrDateUtil.addDay(new Date(), -99);
                }
                Map<String, Object> param = new HashMap<>();
                param.put("date", date1);
                param.put("collectDeviceId", COLLECT_DEVICE_ID);
                int dayCount = statisDeviceDailyGasConsumeCustomDao.select2(param);
                if (dayCount > 0) {
                    StatisDeviceDataExceptionExample exceptionExample = new StatisDeviceDataExceptionExample();
                    exceptionExample.createCriteria().andCollectDeviceIdEqualTo(COLLECT_DEVICE_ID).andExceptionTypeEqualTo(type2).andStatisDateEqualTo(new Date());
                    int count = statisDeviceDataExceptionDao.countByExample(exceptionExample);
                    if (count == 0) {
                        StatisDeviceDataExceptionEntity statisDeviceDataExceptionEntity = new StatisDeviceDataExceptionEntity();
                        statisDeviceDataExceptionEntity.setExceptionDayCount(dayCount > 99 ? 99 : dayCount);
                        statisDeviceDataExceptionEntity.setExceptionType(type2);
                        statisDeviceDataExceptionEntity.setStatisDate(new Date());
                        statisDeviceDataExceptionEntity.setCollectDeviceId(COLLECT_DEVICE_ID);
                        statisDeviceDataExceptionEntity.setCollectDeviceCode(COLLECT_DEVICE_CODE);
                        statisDeviceDataExceptionEntity.setCollectDeviceCategoryCode(COLLECT_DEVICE_CATEGORY_CODE);
                        statisDeviceDataExceptionEntity.setCollectDeviceDeptId(COLLECT_DEVICE_DEPT_ID);
                        statisDeviceDataExceptionEntity.setCollectDeviceDeptName(COLLECT_DEVICE_DEPT_NAME);
                        statisDeviceDataExceptionEntity.setCollectDeviceResourceCode(COLLECT_DEVICE_RESOURCE_CODE);
                        statisDeviceDataExceptionEntity.setIotDeviceId(IOT_DEVICE_ID);
                        statisDeviceDataExceptionEntity.setIotDeviceCode(IOT_DEVICE_CODE);
                        statisDeviceDataExceptionEntity.setIotDeviceCategoryCode(IOT_DEVICE_CATEGORY_CODE);
                        statisDeviceDataExceptionEntity.setIotDeviceCompanyId(IOT_DEVICE_COMPANY_ID);
                        statisDeviceDataExceptionEntity.setIotDeviceCompanyName(IOT_DEVICE_COMPANY_NAME);
                        statisDeviceDataExceptionEntity.setGasUserId(GAS_USER_ID);
                        statisDeviceDataExceptionEntity.setGasUserCode(GAS_USER_CODE);

                        if (statisDeviceDataExceptionEntity.getIotDeviceId() == null ||
                                statisDeviceDataExceptionEntity.getIotDeviceCode() == null ||
                                statisDeviceDataExceptionEntity.getIotDeviceCompanyId() == null ||
                                statisDeviceDataExceptionEntity.getIotDeviceCompanyName() == null) {
                            continue;
                        }

                        insert2.add(statisDeviceDataExceptionEntity);
                        if (insert2.size() == 500) {
                            wyqCount += statisDeviceDataExceptionCustomDao.insertBatch(insert2);
                            insert2 = new ArrayList<>();
                        }
                    }
                }
            } catch (Throwable e) {
                logger.error("同步异常统计异常：", e);
            }
        }


        try {
            if (insert1.size() > 0) {
                wscCount += statisDeviceDataExceptionCustomDao.insertBatch(insert1);
            }
            if (insert2.size() > 0) {
                wyqCount += statisDeviceDataExceptionCustomDao.insertBatch(insert2);
            }
        } catch (Throwable e) {
            logger.error("同步异常统计异常：", e);
        }

        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("wscCount", wscCount);
        resultMap.put("wyqCount", wyqCount);
        Future<Map<String, Integer>> future = new AsyncResult<Map<String, Integer>>(resultMap);// Future接收返回值，这里是String类型，可以指明其他类型
        return future;
    }
    @Async
    public Future<Map<String, Integer>> syncDeviceStatis_onlyDay(int ti, Integer day, List<DevCollectDeviceEntityDto> devices) {
        String gasPressureMonitorCode = this.devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "GAS_PRESSURE_MONITOR");

        int length = 500;
        int threadCount = 10;

        Date startDate = HzhrDateUtil.getDayStartTime(HzhrDateUtil.addDay(new Date(), -day));


        int deviceDailyGasConsumeUpdateCount = 0;
        int deviceDailyGasConsumeInsertCount = 0; int i;
        for (i = ti - 1;; i += threadCount) {

            try { List<StatisDeviceDailyGasConsumeEntity> updateDeviceDailyStatis = new ArrayList<>();
                List<StatisDeviceDailyGasConsumeEntity> insertDeviceDailyStatis = new ArrayList<>();

                List<DevCollectDeviceEntityDto> deviceListTmp = new ArrayList<>();
                for (int j = i * length + 1; j <= i * length + length && j < devices.size(); j++) {
                    DevCollectDeviceEntityDto device = devices.get(j);
                    deviceListTmp.add(device);
                }


                List<DevCollectDeviceEntityDto> deviceList = new ArrayList<>();
                for (DevCollectDeviceEntityDto deviceTmp:
                deviceListTmp) {
                    if (!gasPressureMonitorCode.equals(deviceTmp.getCategoryCode())) {
                        deviceList.add(deviceTmp);
                    }
                }

                for (DevCollectDeviceEntityDto device : deviceList) {
                    Integer deviceId = device.getId();
                    Integer iotDeviceId = device.getIotDeviceId();
                    Integer gasUserId = device.getGasUserId();
                    Integer deptId = device.getDeptId();
                    Integer companyId = device.getCompanyId();
                    Date date = startDate;

                    for (; date.getTime() <= (new Date()).getTime(); date = HzhrDateUtil.addDay(date, 1)) {
                        Double data = getGasConsumeByDayAndCollectDeviceId(date, deviceId);
                        String dataStatus = "1";

                        if (data != null) {
                            if (data > 1.0E7D) {
                                dataStatus = "2";
                            }
                            if (data < 0.0D) {
                                dataStatus = "3";
                            }

                            StatisDeviceDailyGasConsumeExample statisDeviceDailyGasConsumeExample = new StatisDeviceDailyGasConsumeExample();
                            statisDeviceDailyGasConsumeExample.createCriteria().andCollectDeviceIdEqualTo(deviceId).andDateEqualTo(date);
                            List<StatisDeviceDailyGasConsumeEntity> statisDeviceDailyGasConsumeEntities = this.statisDeviceDailyGasConsumeDao.selectByExample(statisDeviceDailyGasConsumeExample);

                            if (statisDeviceDailyGasConsumeEntities.size() > 0) {
                                StatisDeviceDailyGasConsumeEntity statisDeviceDailyGasConsumeEntity = (StatisDeviceDailyGasConsumeEntity)statisDeviceDailyGasConsumeEntities.get(0);
                                statisDeviceDailyGasConsumeEntity.setIotDeviceId(iotDeviceId);
                                statisDeviceDailyGasConsumeEntity.setGasUserId(gasUserId);
                                statisDeviceDailyGasConsumeEntity.setDeptId(deptId);
                                statisDeviceDailyGasConsumeEntity.setCompanyId(companyId);
                                statisDeviceDailyGasConsumeEntity.setData(data);
                                statisDeviceDailyGasConsumeEntity.setDataStatus(dataStatus);
                                updateDeviceDailyStatis.add(statisDeviceDailyGasConsumeEntity);
                                deviceDailyGasConsumeUpdateCount++;
                            } else {
                                StatisDeviceDailyGasConsumeEntity statisDeviceDailyGasConsumeEntity = new StatisDeviceDailyGasConsumeEntity();
                                statisDeviceDailyGasConsumeEntity.setCollectDeviceId(deviceId);
                                statisDeviceDailyGasConsumeEntity.setIotDeviceId(iotDeviceId);
                                statisDeviceDailyGasConsumeEntity.setGasUserId(gasUserId);
                                statisDeviceDailyGasConsumeEntity.setDeptId(deptId);
                                statisDeviceDailyGasConsumeEntity.setCompanyId(companyId);
                                statisDeviceDailyGasConsumeEntity.setData(data);
                                statisDeviceDailyGasConsumeEntity.setDate(date);
                                statisDeviceDailyGasConsumeEntity.setDataStatus(dataStatus);
                                insertDeviceDailyStatis.add(statisDeviceDailyGasConsumeEntity);
                                deviceDailyGasConsumeInsertCount++;
                            }
                        }
                    }
                }

                if (insertDeviceDailyStatis.size() > 0) {
                    this.statisDeviceDailyGasConsumeCustomDao.insertBatch(insertDeviceDailyStatis);
                }
                if (updateDeviceDailyStatis.size() > 0) {
                    this.statisDeviceDailyGasConsumeCustomDao.updateBatch(updateDeviceDailyStatis);
                }

                if (deviceListTmp.size() < length)
                    break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("deviceDailyGasConsumeUpdateCount", deviceDailyGasConsumeUpdateCount);
        resultMap.put("deviceDailyGasConsumeInsertCount", deviceDailyGasConsumeInsertCount);
        return new AsyncResult(resultMap);
    }
    @Async
    public Future<Map<String, Integer>> syncGasUserStatis_onlyDay(int ti, Integer day, List<TollGasUserEntityDto> gasUsers) {
        int length = 500;
        int threadCount = 10;

        Date startDate = HzhrDateUtil.getDayStartTime(HzhrDateUtil.addDay(new Date(), -day.intValue()));


        int gasUserDailyGasConsumeUpdateCount = 0;
        int gasUserDailyGasConsumeInsertCount = 0; int i;
        for (i = ti - 1;; i += threadCount) {

            try { List<StatisGasUserDailyGasConsumeEntity> updateGasUserDailyStatis = new ArrayList<StatisGasUserDailyGasConsumeEntity>();
                List<StatisGasUserDailyGasConsumeEntity> insertGasUserDailyStatis = new ArrayList<StatisGasUserDailyGasConsumeEntity>();

                List<TollGasUserEntityDto> gasUserList = new ArrayList<>();
                for (int j = i * length + 1; j <= i * length + length && j < gasUsers.size(); j++) {
                    TollGasUserEntityDto gasUser = gasUsers.get(j);
                    gasUserList.add(gasUser);
                }

                for (TollGasUserEntityDto gasUser : gasUserList) {
                    Integer gasUserId = gasUser.getId();
                    String gasUserType = gasUser.getTypeCode();
                    Integer deptId = gasUser.getDeptId();
                    Integer companyId = gasUser.getCompanyId();
                    Date date = startDate;

                    for (; date.getTime() <= (new Date()).getTime(); date = HzhrDateUtil.addDay(date, 1)) {
                        Map<String, Object> criteriaMap = new HashMap<String, Object>();
                        criteriaMap.put("gasUserId", gasUserId);
                        criteriaMap.put("startDate", date);
                        Double data = this.statisDeviceDailyGasConsumeCustomDao.selectDataSum(criteriaMap);
                        String dataStatus = "1";

                        if (data != null) {
                            StatisGasUserDailyGasConsumeExample statisGasUserDailyGasConsumeExample = new StatisGasUserDailyGasConsumeExample();
                            statisGasUserDailyGasConsumeExample.createCriteria().andGasUserIdEqualTo(gasUserId).andDateEqualTo(date);
                            List<StatisGasUserDailyGasConsumeEntity> statisGasUserDailyGasConsumeEntities = this.statisGasUserDailyGasConsumeDao.selectByExample(statisGasUserDailyGasConsumeExample);

                            if (statisGasUserDailyGasConsumeEntities.size() > 0) {
                                StatisGasUserDailyGasConsumeEntity statisGasUserDailyGasConsumeEntity = (StatisGasUserDailyGasConsumeEntity)statisGasUserDailyGasConsumeEntities.get(0);
                                statisGasUserDailyGasConsumeEntity.setGasUserId(gasUserId);
                                statisGasUserDailyGasConsumeEntity.setGasUserType(gasUserType);
                                statisGasUserDailyGasConsumeEntity.setDeptId(deptId);
                                statisGasUserDailyGasConsumeEntity.setCompanyId(companyId);
                                statisGasUserDailyGasConsumeEntity.setData(data);
                                statisGasUserDailyGasConsumeEntity.setDataStatus(dataStatus);
                                updateGasUserDailyStatis.add(statisGasUserDailyGasConsumeEntity);
                                gasUserDailyGasConsumeUpdateCount++;
                            } else {
                                StatisGasUserDailyGasConsumeEntity statisGasUserDailyGasConsumeEntity = new StatisGasUserDailyGasConsumeEntity();
                                statisGasUserDailyGasConsumeEntity.setGasUserId(gasUserId);
                                statisGasUserDailyGasConsumeEntity.setGasUserType(gasUserType);
                                statisGasUserDailyGasConsumeEntity.setDeptId(deptId);
                                statisGasUserDailyGasConsumeEntity.setCompanyId(companyId);
                                statisGasUserDailyGasConsumeEntity.setData(data);
                                statisGasUserDailyGasConsumeEntity.setDate(date);
                                statisGasUserDailyGasConsumeEntity.setDataStatus(dataStatus);
                                insertGasUserDailyStatis.add(statisGasUserDailyGasConsumeEntity);
                                gasUserDailyGasConsumeInsertCount++;
                            }
                        }
                    }
                }


                if (insertGasUserDailyStatis.size() > 0) {
                    this.statisGasUserDailyGasConsumeCustomDao.insertBatch(insertGasUserDailyStatis);
                }
                if (updateGasUserDailyStatis.size() > 0) {
                    this.statisGasUserDailyGasConsumeCustomDao.updateBatch(updateGasUserDailyStatis);
                }

                if (gasUserList.size() < length)
                    break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Map<String, Integer> resultMap = new HashMap<String, Integer>();
        resultMap.put("gasUserDailyGasConsumeUpdateCount", Integer.valueOf(gasUserDailyGasConsumeUpdateCount));
        resultMap.put("gasUserDailyGasConsumeInsertCount", Integer.valueOf(gasUserDailyGasConsumeInsertCount));
        return new AsyncResult(resultMap);
    }

    private Double getGasConsumeByDayAndCollectDeviceId(Date day, Integer collectDeviceId) {
        String redisKey = "collectdata:date:" + day.getTime() + ":collectdeviceId:" + collectDeviceId + ":data:";
        Object data = redisUtil.get(redisKey);
        Double dayFirstCollectDataValue = null;
        if (data != null) {
            dayFirstCollectDataValue = Double.parseDouble(data.toString());
        } else {
            DevCollectDataEntity dayFirstCollectDataModel = getFirstCollectDataModelByDayAndCollectDeviceId(day, collectDeviceId);
            if (dayFirstCollectDataModel != null) {
                dayFirstCollectDataValue = dayFirstCollectDataModel.getCurrentValue();
                redisUtil.set(redisKey, dayFirstCollectDataValue, 2L, TimeUnit.HOURS);
            }
        }
        if (dayFirstCollectDataValue == null) return null;

        Date nextDay = HzhrDateUtil.addDay(day, 1);
        for (; nextDay.getTime() < new Date().getTime(); nextDay = HzhrDateUtil.addDay(nextDay, 1) ) {
            redisKey = "collectdata:date:" + nextDay.getTime() + ":collectdeviceId:" + collectDeviceId + ":data:";
            data = redisUtil.get(redisKey);
            Double nextDayFirstCollectDataValue = null;
            if (data != null) {
                nextDayFirstCollectDataValue = Double.parseDouble(data.toString());
            } else {
                DevCollectDataEntity dayFirstCollectDataModel = getFirstCollectDataModelByDayAndCollectDeviceId(nextDay, collectDeviceId);
                if (dayFirstCollectDataModel != null) {
                    nextDayFirstCollectDataValue = dayFirstCollectDataModel.getCurrentValue();
                    redisUtil.set(redisKey, nextDayFirstCollectDataValue, 2L, TimeUnit.HOURS);
                }
            }

            if (nextDayFirstCollectDataValue != null) {
                BigDecimal nextDayFirstCollectDataValueB = new BigDecimal(nextDayFirstCollectDataValue.toString());
                BigDecimal dayFirstCollectDataValueB = new BigDecimal(dayFirstCollectDataValue.toString());
                Double result = new Double(nextDayFirstCollectDataValueB.subtract(dayFirstCollectDataValueB).toString());
                if (result < 0.0) return 0.0;
                return result;
            }
        }
        return null;
    }
    private DevCollectDataEntity getFirstCollectDataModelByDayAndCollectDeviceId(Date day, Integer collectDeviceId) {
        Date nextDay = HzhrDateUtil.addDay(day, 1);
        Date startTime = HzhrDateUtil.getDayStartTime(day);
        Date endTime = HzhrDateUtil.getDayStartTime(nextDay);

        DevCollectDataExample devCollectDataExample = new DevCollectDataExample();
        devCollectDataExample.createCriteria()
                .andCollectDeviceIdEqualTo(collectDeviceId)
                .andCurrentTmGreaterThanOrEqualTo(startTime)
                .andCurrentTmLessThan(endTime);

        List<DevCollectDataEntity> devCollectDataEntities = devCollectDataDao.selectByExample(devCollectDataExample);
        if (devCollectDataEntities.size() > 0) {
            return devCollectDataEntities.get(0);
        }

        return null;
    }
    private List<StatisDeviceDailyGasConsumeEntity> quchong1(List<StatisDeviceDailyGasConsumeEntity> list) {
        //去重
        List<StatisDeviceDailyGasConsumeEntity> addedList = new ArrayList<>();
        List<StatisDeviceDailyGasConsumeEntity> result = new ArrayList<>();
        for (StatisDeviceDailyGasConsumeEntity item:
                list) {
            boolean addFlag = true;

            String eqId = HzhrTypeUtil.object2string(item.getCollectDeviceId());
            if (eqId == null) continue;
            String cdTime = HzhrDateUtil.dateToStr(item.getDate());
            if (cdTime == null) continue;

            for (StatisDeviceDailyGasConsumeEntity addedItem :
                    addedList) {
                String eqId1 = HzhrTypeUtil.object2string(addedItem.getCollectDeviceId());
                String cdTime1 = HzhrDateUtil.dateToStr(addedItem.getDate());

                if (eqId.equals(eqId1) && cdTime.equals(cdTime1)) {
                    addFlag = false;
                    break;
                }
            }

            if (addFlag) {
                result.add(item);
                StatisDeviceDailyGasConsumeEntity addedItem = new StatisDeviceDailyGasConsumeEntity();
                addedItem.setCollectDeviceId(Integer.parseInt(eqId));
                addedItem.setDate(HzhrDateUtil.strToDate(cdTime));
                addedList.add(addedItem);
            }
        }

        return result;
    }
    private List<StatisDeviceMonthlyGasConsumeEntity> quchong2(List<StatisDeviceMonthlyGasConsumeEntity> list) {
        //去重
        List<StatisDeviceMonthlyGasConsumeEntity> addedList = new ArrayList<>();
        List<StatisDeviceMonthlyGasConsumeEntity> result = new ArrayList<>();
        for (StatisDeviceMonthlyGasConsumeEntity item:
                list) {
            boolean addFlag = true;

            String eqId = HzhrTypeUtil.object2string(item.getCollectDeviceId());
            if (eqId == null) continue;
            String cdTime = HzhrDateUtil.dateToStr(item.getDate());
            if (cdTime == null) continue;

            for (StatisDeviceMonthlyGasConsumeEntity addedItem :
                    addedList) {
                String eqId1 = HzhrTypeUtil.object2string(addedItem.getCollectDeviceId());
                String cdTime1 = HzhrDateUtil.dateToStr(addedItem.getDate());

                if (eqId.equals(eqId1) && cdTime.equals(cdTime1)) {
                    addFlag = false;
                    break;
                }
            }

            if (addFlag) {
                result.add(item);
                StatisDeviceMonthlyGasConsumeEntity addedItem = new StatisDeviceMonthlyGasConsumeEntity();
                addedItem.setCollectDeviceId(Integer.parseInt(eqId));
                addedItem.setDate(HzhrDateUtil.strToDate(cdTime));
                addedList.add(addedItem);
            }
        }

        return result;
    }
    private List<StatisGasUserDailyGasConsumeEntity> quchong3(List<StatisGasUserDailyGasConsumeEntity> list) {
        //去重
        List<StatisGasUserDailyGasConsumeEntity> addedList = new ArrayList<>();
        List<StatisGasUserDailyGasConsumeEntity> result = new ArrayList<>();
        for (StatisGasUserDailyGasConsumeEntity item:
                list) {
            boolean addFlag = true;

            String eqId = HzhrTypeUtil.object2string(item.getGasUserId());
            if (eqId == null) continue;
            String cdTime = HzhrDateUtil.dateToStr(item.getDate());
            if (cdTime == null) continue;

            for (StatisGasUserDailyGasConsumeEntity addedItem :
                    addedList) {
                String eqId1 = HzhrTypeUtil.object2string(addedItem.getGasUserId());
                String cdTime1 = HzhrDateUtil.dateToStr(addedItem.getDate());

                if (eqId.equals(eqId1) && cdTime.equals(cdTime1)) {
                    addFlag = false;
                    break;
                }
            }

            if (addFlag) {
                result.add(item);
                StatisGasUserDailyGasConsumeEntity addedItem = new StatisGasUserDailyGasConsumeEntity();
                addedItem.setGasUserId(Integer.parseInt(eqId));
                addedItem.setDate(HzhrDateUtil.strToDate(cdTime));
                addedList.add(addedItem);
            }
        }

        return result;
    }
    private List<StatisGasUserMonthlyGasConsumeEntity> quchong4(List<StatisGasUserMonthlyGasConsumeEntity> list) {
        //去重
        List<StatisGasUserMonthlyGasConsumeEntity> addedList = new ArrayList<>();
        List<StatisGasUserMonthlyGasConsumeEntity> result = new ArrayList<>();
        for (StatisGasUserMonthlyGasConsumeEntity item:
                list) {
            boolean addFlag = true;

            String eqId = HzhrTypeUtil.object2string(item.getGasUserId());
            if (eqId == null) continue;
            String cdTime = HzhrDateUtil.dateToStr(item.getDate());
            if (cdTime == null) continue;

            for (StatisGasUserMonthlyGasConsumeEntity addedItem :
                    addedList) {
                String eqId1 = HzhrTypeUtil.object2string(addedItem.getGasUserId());
                String cdTime1 = HzhrDateUtil.dateToStr(addedItem.getDate());

                if (eqId.equals(eqId1) && cdTime.equals(cdTime1)) {
                    addFlag = false;
                    break;
                }
            }

            if (addFlag) {
                result.add(item);
                StatisGasUserMonthlyGasConsumeEntity addedItem = new StatisGasUserMonthlyGasConsumeEntity();
                addedItem.setGasUserId(Integer.parseInt(eqId));
                addedItem.setDate(HzhrDateUtil.strToDate(cdTime));
                addedList.add(addedItem);
            }
        }

        return result;
    }
    private List<StatisDeptDailyGasConsumeEntity> quchong5(List<StatisDeptDailyGasConsumeEntity> list) {
        //去重
        List<StatisDeptDailyGasConsumeEntity> addedList = new ArrayList<>();
        List<StatisDeptDailyGasConsumeEntity> result = new ArrayList<>();
        for (StatisDeptDailyGasConsumeEntity item:
                list) {
            boolean addFlag = true;

            String eqId = HzhrTypeUtil.object2string(item.getDeptId());
            if (eqId == null) continue;
            String cdTime = HzhrDateUtil.dateToStr(item.getDate());
            if (cdTime == null) continue;

            for (StatisDeptDailyGasConsumeEntity addedItem :
                    addedList) {
                String eqId1 = HzhrTypeUtil.object2string(addedItem.getDeptId());
                String cdTime1 = HzhrDateUtil.dateToStr(addedItem.getDate());

                if (eqId.equals(eqId1) && cdTime.equals(cdTime1)) {
                    addFlag = false;
                    break;
                }
            }

            if (addFlag) {
                result.add(item);
                StatisDeptDailyGasConsumeEntity addedItem = new StatisDeptDailyGasConsumeEntity();
                addedItem.setDeptId(Integer.parseInt(eqId));
                addedItem.setDate(HzhrDateUtil.strToDate(cdTime));
                addedList.add(addedItem);
            }
        }

        return result;
    }
    private List<StatisDeptMonthlyGasConsumeEntity> quchong6(List<StatisDeptMonthlyGasConsumeEntity> list) {
        //去重
        List<StatisDeptMonthlyGasConsumeEntity> addedList = new ArrayList<>();
        List<StatisDeptMonthlyGasConsumeEntity> result = new ArrayList<>();
        for (StatisDeptMonthlyGasConsumeEntity item:
                list) {
            boolean addFlag = true;

            String eqId = HzhrTypeUtil.object2string(item.getDeptId());
            if (eqId == null) continue;
            String cdTime = HzhrDateUtil.dateToStr(item.getDate());
            if (cdTime == null) continue;

            for (StatisDeptMonthlyGasConsumeEntity addedItem :
                    addedList) {
                String eqId1 = HzhrTypeUtil.object2string(addedItem.getDeptId());
                String cdTime1 = HzhrDateUtil.dateToStr(addedItem.getDate());

                if (eqId.equals(eqId1) && cdTime.equals(cdTime1)) {
                    addFlag = false;
                    break;
                }
            }

            if (addFlag) {
                result.add(item);
                StatisDeptMonthlyGasConsumeEntity addedItem = new StatisDeptMonthlyGasConsumeEntity();
                addedItem.setDeptId(Integer.parseInt(eqId));
                addedItem.setDate(HzhrDateUtil.strToDate(cdTime));
                addedList.add(addedItem);
            }
        }

        return result;
    }

}
